<?php
// +----------------------------------------------------------------------
// |优惠券管理
// +----------------------------------------------------------------------
// |Data:2016-06-25 17:34
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Common\Model;
class CouponsModel extends CommonModel{
    protected $tableName = 'coupon';

    /**
     * 获取领取记录
     * @param $map
     * @param int $limit
     * @return bool
     */
    public function getRecordList($map,$limit=10){
        if(empty($map)) return false;
        $list = M()->table(C('DB_PREFIX').'user_info as uf')
                ->join(C('DB_PREFIX').'coupons_receive_logs as cr on uf.uid=cr.uid')
                ->field('uf.nickname,cr.*')
                ->where($map)
                ->order('receive_time desc')
                ->findPage($limit);
        return $list;
    }

    /**
     * 是否领取
     * @param $coupons_id
     * @param $uid
     * @return bool|mixed
     */
    public function hasRecord($coupons_id,$uid){
        if(empty($coupons_id) || empty($uid)) return false;
        return M('coupons_receive_logs')->where('coupons_id='.$coupons_id.' and uid='.$uid)->getField('id');
    }

    /**
     * 添加领取记录
     * @param $data
     * @return bool|mixed
     */
    public function addRecord($data){
        if(empty($data) || !is_array($data)) return false;

        M()->startTrans();
        $res = M('coupons_receive_logs')->add($data);
        $res_2 = $this->where('id='.$data['coupons_id'])->setDec('store');
        if($res && $res_2){
            M()->commit();
            return true;
        }else{
            M()->rollback();
            return false;
        }
    }

    /**
     * 获取用户领取列表
     * @param $map
     * @param int $limit
     * @return bool
     */
    public function getUserRecordList($map,$limit=10){
        if(empty($map) || !is_array($map)) return false;
        $list = M()->table(C('DB_PREFIX').'coupon as c')
                ->join(C('DB_PREFIX').'coupons_receive_logs as cr on c.id=cr.coupons_id')
                ->field('c.id,c.title,denom,max_decom,desc,cr.receive_time,cr.end_time,c.shop_id')
                ->order('cr.receive_time desc')
                ->where($map)
                ->findPage($limit);
        return $list;
    }

    /**
     * 获取店铺优惠券
     * @param $shop_id
     * @return bool|mixed
     */
    public function getShopCouponsList($shop_id){
        if(empty($shop_id)) return false;
        $map = array();
        $map['shop_id'] = $shop_id;
        $map['publish_time'] = array('gt',time());
        $map['is_del'] = 2;

        $list = $this->where($map)->field('id,title,denom,max_decom,desc,store')->select();
        return $list;
    }

    /**
     * 检测用户是否领取
     * @param $uid 用户uid
     * @param $coupons_id 优惠券id
     * @return bool|mixed
     */
    public function hasUseRecord($uid,$coupons_id){
        if(empty($uid) || empty($coupons_id)) return false;
        return M('coupons_receive_logs')->where('uid='.$uid.' and coupons_id='.$coupons_id)->getField('id');
    }

    /**
     * 获取用户
     * @param $shop_id
     * @param $uid
     * @return bool
     */
    public function getShopUserCoupons($shop_id,$uid,$price){
        if(empty($uid) || empty($shop_id)) return false;
        $map = array();
        $map['shop_id'] = $shop_id;
        $map['cr.uid'] = $uid;
        $map['c.is_del'] = 2;
        $map['cr.is_use'] = 2;
        $map['cr.is_overdue'] = 2;
        $map['cr.end_time'] = array('gt',time());
        $map['c.max_decom'] = array('lt',$price);
        $detail = M()->table(C('DB_PREFIX').'coupon as c')
                ->join(C('DB_PREFIX').'coupons_receive_logs as cr on c.id=cr.coupons_id')
                ->field('c.denom,c.id')
                ->where($map)
                ->order('cr.receive_time asc')
                ->find();
        return $detail;

    }
}
?>